package org.nlp2rdf.navigator.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

public class Prefixer {
	private static final Logger logger = Logger.getLogger(Prefixer.class);

	private String base;
	private Map<String, String> prefixToNamespace;
	private Map<String, String> namespaceToPrefix;

	public String nicen(String uri) {
		String name = stripNamespace(uri);
		try {
			name = URLDecoder.decode(name, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			logger.error("", e);
		}

		name = name.replaceAll("_", " ");
		return name;
	}

	/**
	 * strips the namespace, either by base or known prefix or a heuristic
	 * @param uri
	 * @return Ths stripped uri
	 */
	public String stripNamespace(String uri) {
		String name = uri.replace(base, "");
		for (String s : namespaceToPrefix.keySet()) {
			name = name.replace(s, "");
		}
		if (name.equals(uri)) {
			int hash = name.lastIndexOf('#');
			int slash = name.lastIndexOf('/');
			if ((hash == -1 && slash == -1)) {
				// do nothing
			} else if (hash > slash) {
				try {
					name = (hash > slash) ? name.substring(hash) : name.substring(slash);
				} catch (Exception e) {
					logger.error("this string heuristic seems not to be good", e);
				}

			}
		}
		return name;
	}

	/**
	 * 
	 * @param uri
	 * @return The prefixed URI, or the full uri, if no prefix was found
	 */
	public String prefixUri(String uri) {
		String name = uri.replace(base, "");
		for (String s : namespaceToPrefix.keySet()) {
			name = name.replace(s, namespaceToPrefix.get(s) + ":");
		}
		return name;
	}

	public void setPrefixToNamespace(Map<String, String> prefixToNamespace) {
		this.prefixToNamespace = prefixToNamespace;
		this.prefixToNamespace.putAll(theMostPopulars());
		this.namespaceToPrefix = new HashMap<String, String>();
		for (String key : prefixToNamespace.keySet()) {
			namespaceToPrefix.put(prefixToNamespace.get(key), key);
		}

	}

	private Map<String, String> theMostPopulars() {
		Map<String, String> m = new HashMap<String, String>();
		m.put("owl", "http://www.w3.org/2002/07/owl#");
		m.put("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
		m.put("dbo", "http://dbpedia.org/ontology/");
		m.put("dbp", "http://dbpedia.org/property/");
		m.put("foaf", "http://xmlns.com/foaf/0.1/");
		m.put("skos", "http://www.w3.org/2004/02/skos/core#");
		m.put("xsd", "http://www.w3.org/2001/XMLSchema#");
		m.put("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
		m.put("dc", "http://purl.org/dc/elements/1.1/");
		m.put("geo", "http://www.w3.org/2003/01/geo/wgs84_pos#");
		return m;
	}

	public Map<String, String> getPrefixToNamespace() {
		return prefixToNamespace;
	}

	public Map<String, String> getNamespaceToPrefix() {
		return namespaceToPrefix;
	}

	public String getBase() {
		return base;
	}

	public void setBase(String base) {
		this.base = base;
	}

}
